import axios from 'axios'

export const transQuery = (param = {}) => {
  const arr = []
  Object.keys(param).map(key => arr.push(`${key}=${param[key]}`))
  return arr.length ? `?${arr.join('&')}` : ''
}

export default {
  /**
   * 分类管理:
   * 创建分类, 删除分类, 修改分类, 获取所有分类
   */
  createCategory (data) {
    return axios.post('/api/createCategory', data);
  },
  deleteCategoryById (id) {
    return axios.delete(`/api/deleteCategory/${id}`);
  },
  modifyCategory (data) {
    let id = data._id || data.id
    return axios.patch(`/api/modifyCategory/${id}`, data);
  },
  getAllCategorys (data) {
    return axios.get('/api/getAllCategorys' + transQuery(data));
  },
  /**
   * 文件管理:
   * 上传文件, 删除文件, 获取所有文件
   */
  uploadFile (data) {
    return axios.post('/api/uploadFile', data);
  },
  deleteFile (id) {
    return axios.delete(`/api/deleteFile/${id}`);
  },
  getFiles (data) {
    return axios.get('/api/getFiles' + transQuery(data));
  },
}